package com.app.service.impl;

import com.app.entity.Notice;
import com.app.mapper.NoticeMapper;
import com.app.service.NoticeService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author by ltl on 2021/3/18
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    public Result addNotice(Notice notice) {
        if (notice.getId() != null) {
            noticeMapper.updateById(notice);
        } else {
            notice.setCreateTime(new Date());
            noticeMapper.insert(notice);
        }
        return Result.success();
    }

    @Override
    public Result updateNotice(Notice notice) {
        noticeMapper.updateById(notice);
        return Result.success();
    }

    @Override
    public Result findNotice() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state",'1');
        queryWrapper.orderByAsc("weight");
        return Result.success(noticeMapper.selectList(queryWrapper));
    }

    @Override
    public Result activationById(Integer id) {
        Notice notice = noticeMapper.selectById(id);
        if (notice.getState().equals('1')) {
            notice.setState('0');
        } else {
            notice.setState('1');
        }
        noticeMapper.updateById(notice);
        return Result.success();
    }
}
